//
//  HSYRequest.m
//  HSY
//
//  Created by 陈浩 on 2017/3/31.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYRequest.h"
#import "NSString+DeviceInfo.h"
#import "HSYUserCenter.h"

@implementation HSYRequest

- (NSDictionary *)buildUrlParams{
    NSAssert([self serviceCode], @"业务代码不能为空");
    NSAssert(((![self needAccessToken]) || [HSYUserCenter instatce].accessToken.length > 0), @"用户令牌不能为空");
    //基础请求参数
    NSMutableDictionary *params = [@{} mutableCopy];
    params[HSY_COMMON_FIELD_SERVICE_CODE] = [self serviceCode];//业务代码
    params[HSY_COMMON_FIELD_TIME_STAMP] = [NSString currentDateString];//日期字符串
    params[HSY_COMMON_FIELD_V] = API_SERVER_VERSION;//api版本号
    params[HSY_COMMON_FIELD_APP_TYPE] = @"ios";//平台信息
    params[HSY_COMMON_FIELD_DEVICE_ID] = [NSString uuid];//设备uuid
    params[HSY_COMMON_FIELD_ACCESS_TOKEN] = @"accesstoken";

    //用户访问令牌
    if ([self needAccessToken] && [HSYUserCenter instatce].accessToken.length>0) {
        params[HSY_COMMON_FIELD_ACCESS_TOKEN] = [HSYUserCenter instatce].accessToken;
    }
    //用户自定义参数
    if ([self requestData]) {
        params[HSY_COMMON_FIELD_REQUEST_DATA] = [[self requestData] mj_JSONString];
    }
    
    return [params copy];
}

- (NSDictionary *)requestData{
    return nil;
}

- (NSString *)serviceCode{
    return nil;
}

- (NSArray<HSYFileModel *> *)files{
    return nil;
}

- (BOOL)needAccessToken{
    return YES;
}

- (BOOL)isRunning{
    return self.task && self.task.state == NSURLSessionTaskStateRunning;
}

- (void)cancelCurrentTask{
    if ([self isRunning]) {
        [self.task cancel];
        self.task = nil;
    }
}

- (void)addFile:(NSData *)file withFileName:(NSString *)fileName toArray:(NSMutableArray *)array{
    if (file) {
        HSYFileModel *model = [[HSYFileModel alloc] init];
        model.fileData = file;
        model.suffix = @"jpg";
        model.fileType = fileName;
        model.mimeType = @"image/png";
        
        [array addObject:model];
    }
}

- (void)dealloc{
    HSYLog(@"%@ has dealloc", NSStringFromClass([self class]));
}

- (BOOL)fileUplaod{
    return NO;
}

@end
